package com.epsilon.commons.context.delegator;

import org.eclipse.swt.widgets.Shell;

import com.epsilon.commons.context.dao.CommonsContextDao;
import com.epsilon.commons.context.login.events.LoginEventDelegate;
import com.epsilon.commons.context.login.view.LoginShell;

public class LoginEventControlBuilder {
	public static LoginEventControl buildLoginEventControl( Shell parent, Integer idAplicacion,
			CommonsContextDao commonsContextDao, String tituloDelShell, String nombreDeUsuarioLbl, String claveLbl,
			String cancelarBtn, String aceptarBtn ) {

		LoginShell loginShells = new LoginShell( parent );

		if( tituloDelShell != null ) {
			loginShells.setText( tituloDelShell );
		}

		if( nombreDeUsuarioLbl != null ) {
			loginShells.getLoginComposite().getLblUsuario().setText( nombreDeUsuarioLbl );
		}

		if( claveLbl != null ) {
			loginShells.getLoginComposite().getLblClave().setText( claveLbl );
		}

		if( cancelarBtn != null ) {
			loginShells.getLoginComposite().getBtnCancelar().setText( cancelarBtn );
		}

		if( aceptarBtn != null ) {
			loginShells.getLoginComposite().getBtnAceptar().setText( aceptarBtn );
		}

		LoginEventDelegate eventos = new LoginEventDelegate( loginShells, commonsContextDao, idAplicacion );
		setLoginEvents( loginShells, eventos );

		return new LoginEventControl( eventos );
	}

	public static LoginEventControl buildLoginEventControl( Shell parent, Integer idAplicacion,
			CommonsContextDao commonsContextDao ) {
		return buildLoginEventControl( parent, idAplicacion, commonsContextDao, null, null, null, null, null );
	}

	public static LoginEventControl buildLoginEventControl( Shell parent, Integer idAplicacion,
			CommonsContextDao commonsContextDao, String tituloDelShell ) {
		return buildLoginEventControl( parent, idAplicacion, commonsContextDao, tituloDelShell, null, null, null, null );
	}

	private static void setLoginEvents( LoginShell loginShells, LoginEventDelegate eventos ) {
		loginShells.addKeyListener( eventos.getEventoAlPresionarTeclas() );

		loginShells.getLoginComposite().getBtnAceptar().addSelectionListener( eventos.getEventoBtnIngresar() );
		loginShells.getLoginComposite().getBtnAceptar().addKeyListener( eventos.getEventoAlPresionarTeclas() );

		loginShells.getLoginComposite().getBtnCancelar().addSelectionListener( eventos.getEventoBtnCancelar() );
		loginShells.getLoginComposite().getBtnCancelar().addKeyListener( eventos.getEventoAlPresionarTeclas() );

		loginShells.getLoginComposite().getTxtUsuario().addKeyListener( eventos.getEventoAlPresionarTeclas() );
		loginShells.getLoginComposite().getTxtClave().addKeyListener( eventos.getEventoAlPresionarTeclas() );
	}
}